var app = getApp(), fetchApi = require("../../../utils/httpPost.js"), 
i = require("../../../utils/arrayUtil.js")
Page({
  /**
   * 页面的初始数据
   */
  data: {
    hasData:false,
    loading: false,
    netWork: false,
    isLoading: true,
    goodsList:[],
    pageSize:10,
    pageIndex:1,
    mer_id:0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if(options.mer_id){
      this.data.mer_id = parseInt(options.mer_id)
      this.onHttpInit()
    }
  },
  onShow:function(){
  },
  onHttpInit: function () {
    var that = this
    fetchApi.httpPost("/product/list", {
      mer_id: that.data.mer_id,
      pageSize:that.data.pageSize,
      pageIndex: that.data.pageIndex,
      is_mer:1,
    }, function (res) {
      console.log("获取商品列表返回数据:", res)
      if (res.data.code == 0) {
        if (res.data.totalcount > 0) {
          that.data.pageIndex = that.data.pageIndex + 1
          console.log("商家商品列表数据", res)
          var products = res.data.data;
          if(products.length > 0){
            for (var index in products) {
              that.data.goodsList.splice(that.data.goodsList.length, 0, products[index]);
            }
            that.setData({
              hasData: true,
              goodsList: that.data.goodsList
            })
          }
        }
      }
      that.setData({
         isLoading: false, loading: true, netWork: true,
      })
    }, function (a) {
      console.log("获取商品列表返回数据 err:", a);
      that.setData({ hasData: false,  isLoading: false,  loading: true, netWork: true })
    });
  },
  goAddProduct(e){
    wx.navigateTo({
      url: '/pagesMer/pages/product/add/index?mer_id=' + this.data.mer_id + '&product_id=0',
    })
  },
  goEdit(e){
    console.log('goEdit',e);
    var productId = e.detail.currentTarget.dataset.item.id;
    wx.navigateTo({
      url: '/pagesMer/pages/product/add/index?mer_id=' + this.data.mer_id + '&product_id=' + productId,
    })
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.data.pageIndex = 1
    this.setData({
      goodsList:[],
      isLoading:true,
    })
    this.onHttpInit();
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.setData({
      goodsList:[],
      isLoading:true,
    })
    this.onHttpInit();
  },
})